<?php
 
class Lapangan {

	private $ID_kategori_lap;
	private $kategori_lap;
    
	private $ID_lapangan;
    private $nama_lap;
	private $jadwal_lap;
	private $harga_lap;
	private $keterangan_lap;
	private $foto;
    
	private $nama_lapangan;
	
	private $nama;
	private $date;
	private $t;
	private $b;
	private $y;


    private $Host;
    private $MySQLUsername;
    private $MySQLPassword;
    private $Database;
    private $Conn;
    
    //Constructor
    public function Lapangan()
    {
        session_start();
        $this->Host = "localhost";
        $this->MySQLUsername = "root";
        $this->MySQLPassword = "";
        $this->Database = "efutsal";

        $this->Connection();
        
        unset($this->Host);
        unset($this->MySQLUsername);
        unset($this->MySQLPassword);
        unset($this->Database);
    }


    public function Connection()
    {
        $this->Conn = @mysql_connect($this->Host,$this->MySQLUsername,$this->MySQLPassword);
        if($this->Conn)
        {
            mysql_select_db($this->Database) OR die('Could not select DB');
        }
        else
        {
            die(mysql_error());
        }  
    }
    
    public function Query($sql)
    {
        $result = mysql_query($sql); 
        if(!$result)
        {
            die(mysql_error());
        } 
        return $result;
    }
	
    
    public function Disconnect()
    {
        mysql_close($this->Conn);
    }
    
    //Escapes bad values for MySQL to prevent SQL injections.
    public function EscapeString($badstring)
    {
        if(!get_magic_quotes_gpc())
        {
            $goodstring = addslashes($badstring);
        }
        else
        {
            $goodstring = stripslashes($badstring);
        }
        $goodstring = mysql_real_escape_string($badstring);
        return $goodstring;
    }
    
	
 	public function Result_To_Array($result)
    {
        $result_array = array();

        for ($i=0; $row = mysql_fetch_array($result); $i++) 
        {
            $result_array[$i] = $row;
        }

        return $result_array;

    }
	
	/**public function Addfoto($foto)
    {
		$ID = mysql_fetch_array($this->Query("SELECT MAX(ID_foto)+1 from `foto`"));
		$ID_foto = $ID[0];
        $foto = $this->EscapeString($foto);
        $result = $this->Query("INSERT INTO `foto` (ID_foto,foto) VALUES ('$ID_foto','$foto')");
    }**/
	
  public function GetDate(){
	$date = getdate();
	$t = $date['mday'];
	$b = $date['mon'];
	$y = $date['year'];
	
	 $tanggal = "$y-$b-$t";
	 return $tanggal;
	 
 	}		
  
 
 
 public function AddLapangan($user,$nama_lap,$jadwal_lap,$kategori_lap,$stok_lap,$harga_lap,$keterangan_lap,$foto)
    {
	
		$ID = mysql_fetch_array($this->Query("SELECT MAX(ID_lapangan)+1 from `lapangan`"));
		$ID_lapangan = $ID[0];
        $nama_lap = $this->EscapeString($nama_lap);
		$jadwal_lap = $this->EscapeString($jadwal_lap);
		$ID_k = mysql_fetch_array($this->Query("SELECT ID_kategori_lap from `kategori_lapangan` where kategori_lap='$kategori_lap'"));
		$ID_kategori=$ID_k[0];
		$harga_lap = $this->EscapeString($harga_lap);
		$keterangan_lap = $this->EscapeString($keterangan_lap);
		$foto= $this->EscapeString($foto);
		$ID_u=mysql_fetch_array($this->Query("SELECT ID_user from `user` where username='$user'"));
		$ID_user=$ID_u[0];
		
        $result = $this->Query("INSERT INTO `lapangan` (ID_lapangan,ID_kategori_lap,nama_lap,jadwal_lap,harga_lap,keterangan_lap,foto_lap,id_user) VALUES ('$ID_lapangan','$ID_kategori','$nama_lap','$jadwal_lap','$harga_lap','$keterangan_lap','$foto','$ID_user')");
		header('location:../page/HOME_sewa.php'); 
    }
	

  public function AddTransaksi($user,$tanggal_sewa,$nama_lap)
	{
	$ID = mysql_fetch_array($this->Query("SELECT MAX(ID_sewa)+1 from `sewa_lapangan`"));
	$ID_sewa = $ID[0];
	$ID_u=mysql_fetch_array($this->Query("SELECT ID_user from `user` where username='$user'"));
	$ID_user=$ID_u[0];
	$tanggal_sewa=$this->EscapeString($tanggal_sewa);
	$ID_l=mysql_fetch_array($this->Query("SELECT ID_lapangan from `lapangan` where nama_lap='$nama_lap'"));
	$ID_lapangan=$ID_l[0];
	
	$result = $this->Query("INSERT INTO  `sewa_lapangan` ( ID_sewa, ID_user, ID_lapangan, tanggal_sewa ) VALUES ('$ID_sewa',  '$ID_user',  '$ID_lapangan',  '$tanggal_sewa')");
		header('location:../page/view_sewa.php'); 	
	}
	
	
  public function UpdateStlap($nama_lap)
	{
	$ID_l=mysql_fetch_array($this->Query("SELECT ID_lapangan from `lapangan` where nama_lap='$nama_lap'"));
	$ID_lapangan=$ID_l[0];
	
	$result = $this->Query("UPDATE `lapangan` SET status ='1' WHERE ID_lapangan='$ID_lapangan'");	
	}

	
	}